package com.zjlp.seproject.controller;

import com.zjlp.seproject.pojo.ID;
import com.zjlp.seproject.pojo.Profile;
import com.zjlp.seproject.pojo.Subject;
import com.zjlp.seproject.pojo.User;
import com.zjlp.seproject.service.ProfileService;
import com.zjlp.seproject.service.SubjectService;
import com.zjlp.seproject.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@RestController
public class ProfileController {
    @Autowired
    private ProfileService profileService;
    @Autowired
    private SubjectService subjectService;

    /**
     *
     * @param profile  传入Profile对象
     * @param authorization  请求头token
     */
    @RequestMapping("/setProfile")
    public void setProfile(@RequestBody Profile profile, @RequestHeader("Authorization") String authorization) {
        System.out.println("setProfile");
        profile.setUid(Integer.parseInt(JwtUtil.getUserId(authorization)));
        profileService.setProfile(profile);
    }

    /**
     *
     * @param id  传入用户id
     * @return  返回Profile对象
     */
    @RequestMapping("/getProfile")
    public Profile getProfile(@RequestBody ID id) {
        System.out.println("getProfile");
        return profileService.getProfile(id.getId());
    }

    /**
     *
     * @param authorization  请求头token
     * @param response   相应
     * @return  返回Subject列表
     */
    @RequestMapping("/personalSubject")
    public List<Subject> personalSubject(@RequestHeader("Authorization") String authorization, HttpServletResponse response) {
        System.out.println("personalSubject");
        if (!JwtUtil.verify(authorization)) {
            response.setStatus(999);
            return null;
        }
        String id = JwtUtil.getUserId(authorization);
        return subjectService.personalSubject(Integer.parseInt(id));
    }
}
